package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    /**
     * 分页查询分类品牌列表
     *
     * @return
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
        return baseMapper.selectCategoryBrandList();
    }

    /**
     * 保存分类品牌
     *
     * @param categoryBrand
     */
    @Override
    public int saveCategoryBrand(CategoryBrand categoryBrand) {
        //1.根据提交分类ID+品牌ID查询是否存在，如果存在则抛出异常
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        queryWrapper.eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId());
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("该品牌已关联该分类");
        }
        //2.保存分类品牌
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return baseMapper.insert(categoryBrand);
    }

    /**
     * 根据三级分类ID查询品牌列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return baseMapper.selectBrandListByCategoryId(categoryId);
    }
}
